package com.example.sbbsec.quartz.schedule;

import com.example.sbbsec.quartz.entity.schedule.ScheduleJob;
import com.example.sbbsec.quartz.util.QuartzUtil;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * 定时任务接口服务
 *
 * @author lowe.yang
 * @date 2019-01-25 16:43
 */
public abstract class BaseJobService<T extends ScheduleJob> implements IJobService<T> {
    private static final Logger LOGGER = getLogger(BaseJobService.class);

    @Autowired
    protected Scheduler scheduler;

    @Override
    public Boolean createJob(T job) {
        //参数校验
        checkJob(job);

        try {
            //构建job信息
            final JobDetail jobDetail = QuartzUtil.buildJobDetail(job);
            //添加JobDataMap数据
            jobDetail.getJobDataMap().putAll(job.getData());

            //触发器
            final Trigger trigger = buildTrigger(job);

            //创建任务
            scheduler.scheduleJob(jobDetail, trigger);

            return Boolean.TRUE;
        } catch (Exception e) {
            LOGGER.error("创建任务失败:{}", e);
            return Boolean.FALSE;
        }
    }

    /**
     * 构建触发器
     * 需要子类实现
     *
     * @param job 任务
     * @return 触发器
     */
    protected abstract Trigger buildTrigger(T job);

    @Override
    public Boolean updateJob(T job) {

        try {
            //删除
            deleteJob(job.getOldJobName(), job.getOldJobGroup());
            //创建
            return createJob(job);
        } catch (Exception e) {
            LOGGER.error("更新任务失败:{}", e);
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean deleteJob(String jobName) {
        return deleteJob(jobName, Scheduler.DEFAULT_GROUP);
    }

    @Override
    public Boolean deleteJob(String jobName, String jobGroup) {
        //参数校验
        checkBaseInfo(jobName, jobGroup);

        try {
            return scheduler.deleteJob(QuartzUtil.buildJobKey(jobName, jobGroup));
        } catch (Exception e) {
            LOGGER.error("删除任务失败:{}", e);
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean pauseJob(String jobName) {
        return pauseJob(jobName, Scheduler.DEFAULT_GROUP);
    }

    @Override
    public Boolean pauseJob(String jobName, String jobGroup) {
        //参数校验
        checkBaseInfo(jobName, jobGroup);

        try {
            scheduler.pauseJob(QuartzUtil.buildJobKey(jobName, jobGroup));
            return Boolean.TRUE;
        } catch (Exception e) {
            LOGGER.error("暂停任务失败:{}", e);
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean resumeJob(String jobName) {
        return resumeJob(jobName, Scheduler.DEFAULT_GROUP);
    }

    @Override
    public Boolean resumeJob(String jobName, String jobGroup) {
        //参数校验
        checkBaseInfo(jobName, jobGroup);

        try {
            scheduler.resumeJob(QuartzUtil.buildJobKey(jobName, jobGroup));
            return Boolean.TRUE;
        } catch (Exception e) {
            LOGGER.error("恢复任务失败:{}", e);
            return Boolean.FALSE;
        }
    }

    /**
     * 校验
     */
    protected void checkBaseInfo(String jobName, String jobGroup) {
        if (isNullOrEmpty(jobName)) {
            throw new IllegalArgumentException("jobName can not be empty");
        }
        if (isNullOrEmpty(jobGroup)) {
            throw new IllegalArgumentException("jobGroup can not be empty");
        }
    }

    /**
     * 校验
     */
    protected void checkJob(T job) {
        if (job == null) {
            throw new IllegalArgumentException("job can not be null");
        }

        checkBaseInfo(job.getJobName(), job.getJobGroup());

        if (job.getExecutor() == null) {
            throw new IllegalArgumentException("executor can not be null");
        }
    }

}
